package com.ewzj.bigdata.mvp.presenter

import android.app.Application
import android.support.v7.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.utils.RxUtils
import com.ewzj.bigdata.mvp.contract.RealNewsFragmentContract
import com.ewzj.bigdata.mvp.model.reqbean.Reqpagebean
import com.ewzj.bigdata.mvp.model.rspbean.CityBean
import com.ewzj.bigdata.mvp.model.rspbean.InfoBean
import com.ewzj.bigdata.mvp.model.rspbean.InfosData
import com.ewzj.bigdata.mvp.ui.adapter.InfoAdapter
import com.google.gson.Gson
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.utils.DataHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class RealNewsFragmentPresenter @Inject constructor(val application: Application, val mGson:Gson,val rxErrorhadler:RxErrorHandler, view:RealNewsFragmentContract.View, model:RealNewsFragmentContract.Model):BasePresenter<RealNewsFragmentContract.Model,RealNewsFragmentContract.View>(model,view){
    val datas= arrayListOf<InfoBean>()
    val adapter by lazy { InfoAdapter(datas) }
    var page=1;
    var locationcity: String = ""
    fun initAdapter(recyclerView: RecyclerView):Unit{
        recyclerView.adapter=adapter
        adapter.setOnLoadMoreListener(object :BaseQuickAdapter.RequestLoadMoreListener{
            override fun onLoadMoreRequested() {
                loadmoreData()
            }

        },recyclerView)
//        mRootView.initRecylerviewAdapter(adapter)
    }

    fun initFirstData():Unit{
        page=1
        mModel.getInfos(Reqpagebean()).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<InfosData>(rxErrorhadler){
                    override fun onNext(t: InfosData) {
                        page++
                        adapter.setNewData(t.list)
                        if (t.list.size<AppKey.PAGE_NUM){
                            adapter.disableLoadMoreIfNotFullPage()
                        }
                    }

                })
    }

    fun loadmoreData():Unit{
        mModel.getInfos(Reqpagebean(page)).compose(RxUtils.bindToLifecycle(mRootView))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doAfterTerminate { if (adapter.isLoading){
                    adapter.loadMoreComplete()
                } }
                .subscribe(object :Consumer<InfosData>{
                    override fun accept(t: InfosData) {
                        page++
                        if (t.list.size<AppKey.PAGE_NUM){
                            adapter.addData(t.list)
                            adapter.loadMoreEnd()
                        }else{
                            adapter.addData(t.list)
                        }
                    }

                },Consumer { adapter.loadMoreFail() })
    }

    fun onreusme(){
            var spcity = DataHelper.getStringSF(application, AppKey.LOCATION_CITY)
            if (spcity.isNullOrEmpty()) return
            if (!locationcity.equals(spcity)) {
                var citybean = mGson.fromJson(spcity, CityBean.ListEntity::class.java)
                initFirstData()
                locationcity = DataHelper.getStringSF(application, AppKey.LOCATION_CITY)
            }
    }
}